/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.izu.mytruco.service;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ademar
 */
public class CommunicatorServer implements Runnable {

    protected Thread thread;
    protected ServerSocket server;
    protected List<CommunicatorServerClient> clients;

    public CommunicatorServer(int port) throws IOException {
        this.thread = new Thread(this);
        this.clients = Collections.synchronizedList(new ArrayList<CommunicatorServerClient>());
        this.server = new ServerSocket(port);
        this.thread.start();
    }

    public void run() {
        while(true) {
            try {
                clients.add(new CommunicatorServerClient(server.accept()));
                System.out.println("Client connectado" + clients.size());
            } catch (IOException ex) {
                Logger.getLogger(CommunicatorServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        CommunicatorServer server = new CommunicatorServer(8888);
    }
}
